package com.twu.biblioteca;

import java.util.ArrayList;

/**
 * Created by mauricio on 9/22/14.
 */
public class Book extends Checkable {
    private String title;
    private String author;
    private String year;

    public static ArrayList<Book> resource = new ArrayList<Book>();
    private  int id;

    public Book(String title, String year, String author) {
        this.title = title;
        this.author = author;
        this.year = year;

        this.id = resource.size();
    }
    public Book() {
        this.id = resource.size();
    }

    public int getId() {
        return this.id;
    }
    public String getTitle() {
        return this.title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public static ArrayList<Book> getAll() {
        if(resource.size() == 0) {
            resource.add(new Book("TDD by Example", "2002", "Jonas Melo"));
            resource.add(new Book("Agile Explained", "2016", "Joventina Silva"));
        }

        return resource;
    }

    public String getAuthor() {
        return this.author;
    }
    
    public void setAuthor(String author) {
        this.author = author;
    }

    public String getYear() {
        return this.year;
    }

    public void setYear(String year) {
        this.year = year;
    }



    public static boolean isValid(Object obj) {
        int num;
        try {
            num = Integer.parseInt(obj.toString());
        }
        catch(NumberFormatException nfe) {
            return false;
        }

        if ((num < resource.size() && num > 0) || num == 0 ) {
            return true;
        }

        return false;
    }



    public String toString() {
        return "[" + getId() + "] " +  getTitle() + " - " + getYear() + " - " + getAuthor();
    }


}
